FROM debian:bullseye

# 替换debian源为国内镜像源
RUN set -ex \
    && sed -i 's|deb.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list  \    
    && sed -i 's|security.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list \
    && apt-get update



RUN apt-get install -y default-libmysqlclient-dev build-essential curl unzip tzdata  \
    && dpkg-reconfigure --frontend noninteractive tzdata    



# 设置时区为北京时间（Asia/Shanghai）
ENV TZ=Asia/Shanghai
# 更新时区文件并配置时区
RUN echo "$TZ" > /etc/timezone && \
    ln -fs /usr/share/zoneinfo/$TZ /etc/localtime 


# 下载并安装Miniconda
RUN curl -so miniconda_installer.sh \
    https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
    chmod +x miniconda_installer.sh && \
    ./miniconda_installer.sh -b -p /opt/conda && \
    rm miniconda_installer.sh && \
     echo ". /opt/conda/etc/profile.d/conda.sh" >> /root/.bashrc

# 创建conda虚拟环境
RUN /opt/conda/bin/conda create -y --name py310 python=3.11
# #更新pip源
RUN mkdir ~/.pip \
    && echo "[global]" >>~/.pip/pip.conf \
    && echo "index-url=https://pypi.tuna.tsinghua.edu.cn/simple" >>~/.pip/pip.conf \    
    && /opt/conda/envs/py310/bin/pip install --upgrade pip 

# 首先安装poetry
RUN /opt/conda/envs/py310/bin/pip install poetry --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple    
RUN /opt/conda/envs/py310/bin/poetry config virtualenvs.create false


WORKDIR /app/


# Copy poetry.lock* in case it doesn't exist in the repo
COPY ./pyproject.toml ./poetry.lock* /app/

# Allow installing dev dependencies to run tests

RUN /opt/conda/envs/py310/bin/poetry install --no-root --only main

# 清理工作
RUN apt-get purge -y build-essential \
    && apt-get autoremove -y \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*


ENV PYTHONPATH=/app

COPY  ./static /app/static
COPY  ./.env.prd /app/.env.prd
COPY  ./app /app/app


ENV PATH=/opt/conda/envs/py310/bin:$PATH
ENV PYTHONUNBUFFERED=1
ENV ENV=prd


# 创建非root用户
RUN groupadd -r appuser && useradd -r -g appuser appuser
# 设置适当的权限
RUN chown -R appuser:appuser /app

# 切换到非root用户
USER appuser
CMD uvicorn main:app --workers 2 --host 0.0.0.0 --port 7860 --app-dir ./app


